import http from './request.js'

/**
 * 进一步封装普通get请求
 * 后台接口返回类型必须带有code=1(是否请求成功，哪怕返回数据为空) 属性
 * @param params 查询条件
 * @param url 地址
 * @param okcb 调用成功的回调
 * @param failcb  调用失败的回调
 */
export const commonGet = (url, params, okcb, failcb) => {
  http.get(url, {
    params: params
  }).then(response => {
    let code = response.code || 0
    let body = response.data || []
    if (code >= 200 && code <= 204) {
      console.log('[GET]-OK with ' + url)
      return okcb(body)
    } else {
      console.log('[GET]-FAIL with ' + url)
      return failcb(body)
    }
  }).catch(trace => failcb(trace.response))
}
/**
 * 封装post方法(JSON)
 * @param url
 * @param postData
 * @param okcb
 * @param failcb
 */
export const commonPost = (url, postData, okcb, failcb) => {
  // console.log('postData: ', postData)
  http.post(url, postData, {
    headers: {
      'Content-Type': 'application/json'
    }
  }).then(response => {
    let status = response.status || 0
    let body = response.data || []
    if ((status >= 200 && status <= 204)) {
      console.log('[POST]-OK with ' + url)
      return okcb(body)
    } else {
      console.log('[POST]-FAIL with ' + url)
      return failcb(body)
    }
  }).catch(trace => failcb(trace.response))
}

export const commonUpload = (url, fileForm, okcb, failcb) => {
  // console.log('fileForm', fileForm)
  http.post(url, fileForm, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(response => {
    let status = response.status || 0
    let body = response.data || []
    if ((status >= 200 && status <= 204) && body.code === '1') {
      console.log('[POST]-OK with ' + url)
      return okcb(body)
    } else {
      console.log('[POST]-FAIL with ' + url)
      return failcb(body)
    }
  }).catch(trace => failcb(trace.response))
}

export const commonDownload = (url, postData, okcb, failcb) => {
  http.get(url, {
    params: postData,
    responseType: 'arraybuffer'
  }).then(response => {
    let status = response.status || 0
    let body = response.data || []
    console.log('response', response)
    console.log('body', body)
    if (status >= 200 && status <= 204) {
      console.log('[DOWNLOAD]-OK with ' + url)
      return okcb(body)
    } else {
      console.log('[DOWNLOAD]-FAIL with ' + url)
      return failcb(body)
    }
  }).catch(trace => failcb(trace.response))
}

export const commonPostDownload = (url, postData, okcb, failcb) => {
  http.post(url, postData, {
    headers: {
      'Content-Type': 'application/json'
    },
    responseType: 'arraybuffer'
  }).then(response => {
    let status = response.status || 0
    let body = response.data || []
    console.log('response', response)
    console.log('body', body)
    if (status >= 200 && status <= 204) {
      console.log('[DOWNLOAD]-OK with ' + url)
      return okcb(body)
    } else {
      console.log('[DOWNLOAD]-FAIL with ' + url)
      return failcb(body)
    }
  }).catch(trace => failcb(trace.response))
}

export const commonPostDownloadBold = (url, postData, okcb, failcb) => {
  http.post(url, postData, {
    headers: {
      'Content-Type': 'application/json'
    },
    responseType: 'blob'
  }).then(response => {
    let status = response.status || 0
    let body = response.data || []
    if (status >= 200 && status <= 204) {
      console.log('[DOWNLOAD]-OK with ' + url)
      return okcb(body)
    } else {
      console.log('[DOWNLOAD]-FAIL with ' + url)
      return failcb(body)
    }
  }).catch(trace => failcb(trace.response))
}

export const getPrint = (url, okcb, failcb) => {
  http.get(url, {
    responseType: 'blob'
  }).then(response => {
    let status = response.status || 0
    if ((status >= 200 && status <= 204)) {
      console.log('[GET PRINT]-OK')
      return okcb(new Blob([response.data], { type: 'application/pdf' }))
    } else {
      console.log('[GET PRINT]-FAIL')
      return failcb()
    }
  }).catch(trace => failcb(trace.response))
}
export const postPrint = (url, { form }, okcb, failcb) => {
  const postData = JSON.stringify(form)
  http.post(url, postData, {
    headers: {
      'Content-Type': 'application/json'
    },
    responseType: 'blob'
  }).then(response => {
    let status = response.status || 0
    if ((status >= 200 && status <= 204)) {
      console.log('[GET PRINT]-OK')
      return okcb()
    } else {
      console.log('[GET PRINT]-FAIL')
      return failcb()
    }
  }).catch(trace => failcb(trace.response))
}
